package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeRisk;
import com.ruoyi.food.mapper.OpmFoodSafeRiskMapper;
import com.ruoyi.food.service.IOpmFoodSafeRiskService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业食品质量安全风险Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeRiskServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeRiskService {
    @Autowired
    private OpmFoodSafeRiskMapper opmFoodSafeRiskMapper;

    /**
     * 查询企业食品质量安全风险
     *
     * @param id 企业食品质量安全风险ID
     * @return 企业食品质量安全风险
     */
    @Override
    public OpmFoodSafeRisk selectOpmFoodSafeRiskById(Long id) {
        OpmFoodSafeRisk result = opmFoodSafeRiskMapper.selectOpmFoodSafeRiskById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询企业食品质量安全风险列表
     *
     * @param opmFoodSafeRisk 企业食品质量安全风险
     * @return 企业食品质量安全风险
     */
    @Override
    public List<OpmFoodSafeRisk> selectOpmFoodSafeRiskList(OpmFoodSafeRisk opmFoodSafeRisk) {
        return opmFoodSafeRiskMapper.selectOpmFoodSafeRiskList(opmFoodSafeRisk);
    }

    /**
     * 新增企业食品质量安全风险
     *
     * @param opmFoodSafeRisk 企业食品质量安全风险
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeRisk(OpmFoodSafeRisk opmFoodSafeRisk) {
        opmFoodSafeRisk.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeRisk.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeRisk.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeRisk.getFileIdList(), opmFoodSafeRisk.getId().toString());
        return opmFoodSafeRiskMapper.insertOpmFoodSafeRisk(opmFoodSafeRisk);
    }

    /**
     * 修改企业食品质量安全风险
     *
     * @param opmFoodSafeRisk 企业食品质量安全风险
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeRisk(OpmFoodSafeRisk opmFoodSafeRisk) {
        opmFoodSafeRisk.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeRisk.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeRisk.getFileIdList(), opmFoodSafeRisk.getId().toString());
        return opmFoodSafeRiskMapper.updateOpmFoodSafeRisk(opmFoodSafeRisk);
    }

    /**
     * 批量删除企业食品质量安全风险
     *
     * @param ids 需要删除的企业食品质量安全风险ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRiskByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeRiskMapper.deleteOpmFoodSafeRiskByIds(ids);
    }

    /**
     * 删除企业食品质量安全风险信息
     *
     * @param id 企业食品质量安全风险ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRiskById(Long id) {
        return opmFoodSafeRiskMapper.deleteOpmFoodSafeRiskById(id);
    }
}
